package com.qfedu.mesadmin.md.unit.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qfedu.mes.common.PageVO;
import com.qfedu.mes.entity.md.MdUnitMeasure;
import com.qfedu.mesadmin.md.unit.dao.MdUnitMeasureDao;
import com.qfedu.mesadmin.md.unit.params.UnitPageParam;
import com.qfedu.mesadmin.md.unit.service.MdUnitMeasureService;
import com.qfedu.mesadmin.md.unit.vo.PrimaryUnitVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MdUnitMeasureServiceImpl implements MdUnitMeasureService {

    @Resource
    private MdUnitMeasureDao mdUnitMeasureDao;

    @Override
    public List<MdUnitMeasure> listUnits() {
        return mdUnitMeasureDao.selectList();
    }

    @Override
    public PageVO<MdUnitMeasure> pageUnit(UnitPageParam param) {
        // 分页插件指定页码和每页显示记录数
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        // 紧跟着调用查询所有记录的方法，返回的是Page对象
        List<MdUnitMeasure> mdUnitMeasures = mdUnitMeasureDao.selectListByInfo(param);

        Page pageInfo = (Page) mdUnitMeasures;

        PageVO<MdUnitMeasure> pageVO = new PageVO<>();
        pageVO.setTotal(pageInfo.getTotal());
        pageVO.setDataList(mdUnitMeasures);

        return pageVO;
    }

    @Override
    public void addUnit(MdUnitMeasure mdUnitMeasure) {
        // 一般需要判断单位的编码和单位名称不能相同

        mdUnitMeasureDao.insert(mdUnitMeasure);
    }

    @Override
    public List<PrimaryUnitVO> listPrimaryUnit() {
        return mdUnitMeasureDao.selectPrimary();
    }

    @Override
    public List<PrimaryUnitVO> listAllUnit() {
        return mdUnitMeasureDao.selectAll();
    }

    @Override
    public void deleteUnit(Integer id) {
        // 主单位能不能直接删除
        // 如果主单位有子单位，提示给前端

        mdUnitMeasureDao.deleteById(id);
    }

    @Override
    public void deleteUnitBatch(List<Integer> ids) {
        // 如果主单位有子单位，提示给前端

        mdUnitMeasureDao.deleteBatch(ids);
    }

    @Override
    public MdUnitMeasure unitInfo(Integer id) {
        return mdUnitMeasureDao.selectById(id);
    }

    @Override
    public void updateUnit(MdUnitMeasure mdUnitMeasure) {
        mdUnitMeasureDao.update(mdUnitMeasure);
    }
}
